﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FimetContoDeposito.BLL.Objects;
using FimetContoDeposito.DAL;
using GrayParrot.Core;

namespace FimetContoDeposito.BLL.Facades
{
    public class CustomerOperatorsFacade : FacadeBase<CustomerOperatorsFacade>
    {
        FCDContext m_ctx = null;

        public Repository<FCD_CustomersOperator, Guid> Repo { get; internal set; }


        public CustomerOperatorsFacade()
        {
        }

        public override void Initialize()
        {
            m_ctx = new FCDContext();
            m_ctx.Configuration.LazyLoadingEnabled = false;
            m_ctx.Configuration.ProxyCreationEnabled = false;

            Repo = this.GetRepository<FCD_CustomersOperator, Guid>(m_ctx);
        }

        public override void Dispose()
        {
            if (m_ctx != null)
            {
                m_ctx.Dispose();

                Repo.Dispose();
            }
        }

        public ReturnValue<bool> Add(FCD_CustomersOperator item)
        {
            ReturnValue<bool> retValue = new ReturnValue<bool>();

            try
            {
                if (item.Id == Guid.Empty)
                {
                    item.Id = Guid.NewGuid();
                }

                this.Repo.Add(item);
                this.Repo.Save();

                retValue.HasValue = true;
                retValue.Value = true;
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }

        public ReturnValue<bool> Edit(FCD_CustomersOperator item)
        {
            ReturnValue<bool> retValue = new ReturnValue<bool>();

            try
            {
                this.Repo.Edit(item);
                this.Repo.Save();

                retValue.HasValue = true;
                retValue.Value = true;
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }

        public ReturnValue<bool> Delete(FCD_CustomersOperator item)
        {
            ReturnValue<bool> retValue = new ReturnValue<bool>();

            try
            {
                this.Repo.Delete(item);
                this.Repo.Save();

                retValue.HasValue = true;
                retValue.Value = true;
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }

        public ReturnValue<FCD_CustomersOperator> Get(Guid id)
        {
            ReturnValue<FCD_CustomersOperator> retValue = new ReturnValue<FCD_CustomersOperator>();

            try
            {
                retValue.Value = this.Repo.Get(id);
                retValue.HasValue = true;
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }

        public ReturnValue<IQueryable<FCD_CustomersOperator>> GetAll()
        {
            ReturnValue<IQueryable<FCD_CustomersOperator>> retValue = new ReturnValue<IQueryable<FCD_CustomersOperator>>();
            retValue.HasValue = false;
            retValue.Value = null;

            try
            {
                retValue.Value = this.Repo.GetAll();
                retValue.HasValue = true;
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }

        public ReturnValue<IQueryable<FCD_CustomersOperator>> GetNewOperators()
        {
            ReturnValue<IQueryable<FCD_CustomersOperator>> retValue = new ReturnValue<IQueryable<FCD_CustomersOperator>>();
            retValue.HasValue = false;
            retValue.Value = null;

            try
            {
                retValue.Value = this.Repo.GetAll().Where(x=>x.CustomerId == null);
                retValue.HasValue = true;
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }

        public ReturnValue<IQueryable<CustomerOperatorBag>> GetCustomerOperatorBags()
        {
            ReturnValue<IQueryable<CustomerOperatorBag>> retValue = new ReturnValue<IQueryable<CustomerOperatorBag>>();
            retValue.HasValue = false;
            retValue.Value = null;

            try
            {
                var objs = from x in m_ctx.FCD_VwCustomerOperators
                           select new CustomerOperatorBag() { 
                            Id = x.Id,
                            Nome = x.Nome,
                            Cognome = x.Cognome,
                            Email = x.Email,
                            Customer = x.AziendaId,
                            Azienda = x.RagioneSociale,
                            IsApprovato =x.IsApproved,
                            Note = x.Note,
                            Username = x.Username
                           };

                if (objs.Any())
                {
                    retValue.Value = objs;
                    retValue.HasValue = true;
                }
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }

        public ReturnValue<CustomerOperatorBag> GetCustomerOperatorBags(Guid id)
        {
            ReturnValue<CustomerOperatorBag> retValue = new ReturnValue<CustomerOperatorBag>();

            try
            {
                var objs = from x in m_ctx.FCD_VwCustomerOperators
                           where x.Id == id
                           select new CustomerOperatorBag()
                           {
                               Id = x.Id,
                               Nome = x.Nome,
                               Cognome = x.Cognome,
                               Email = x.Email,
                               Customer= x.AziendaId,
                               Azienda = x.RagioneSociale,
                               IsApprovato = x.IsApproved,
                               Note = x.Note,
                               Username = x.Username
                           };

                if (objs.Any())
                {
                    retValue.Value = objs.FirstOrDefault();
                    retValue.HasValue = true;
                }
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }

        public ReturnValue<CustomerOperatorBag> GetCustomerOperatorBags(string userName)
        {
            ReturnValue<CustomerOperatorBag> retValue = new ReturnValue<CustomerOperatorBag>();

            try
            {
                var objs = from x in m_ctx.FCD_VwCustomerOperators
                           where x.Username.ToLower() == userName.ToLower()
                           select new CustomerOperatorBag()
                           {
                               Id = x.Id,
                               Nome = x.Nome,
                               Cognome = x.Cognome,
                               Email = x.Email,
                               Customer = x.AziendaId,
                               Azienda = x.RagioneSociale,
                               IsApprovato = x.IsApproved,
                               Note = x.Note,
                               Username = x.Username
                           };

                if (objs.Any())
                {
                    retValue.Value = objs.FirstOrDefault();
                    retValue.HasValue = true;
                }
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }
    }
}
